package com.xingdata.jjxc.views;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 自定义处理长按事件
 * Created by lucian on 16-12-11.
 */

public class LongPressImageView extends ImageView {
    long key_down_time;
    long key_up_time;
    String TAG = "LongPressImageView";
    Handler h = new Handler();

    public LongPressImageView(Context context) {
        super(context);
        Log.d(TAG,"ctor 1"  );
    }

    public LongPressImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG,"ctor 2"  );
    }

    public LongPressImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.d(TAG,"ctor 3"  );
    }
    MyTimer timer;
    int longClickCount = 0;

    public int getLongClickCount(){
        return longClickCount;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG,"pointer:" + event.getPointerCount());
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                key_down_time = System.currentTimeMillis();
                Log.d(TAG,"DDDDDDDDDown");
                if(timer == null)
                    timer = new MyTimer(50, new Timeout() {
                        @Override
                        public void onTimeout() {
                            h.post(new Runnable() {
                                @Override
                                public void run() {
                                    longClickCount ++;
                                    performLongClick();
                                }
                            });
                            Log.i(TAG,"===============+++>LONG CLICK ..........");
                            //timer.accelerate();// 加速长按
                        }
                    });
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG,"UPUPUPUP");
                if(System.currentTimeMillis() - key_down_time < 1000){
                    performClick(); // 短按事件
                }
                timer.cancel();
                timer = null;
                longClickCount = 0;
                return true;
            default:
                break;
        }
        return true;
    }

    class MyTimer extends TimerTask {
        Timeout callback;
        Timer timer;
        int interval = 50;
        int timeout = 1000;//1s default
        int acc_level = 0;
        int[] speedUp = new int[]{30,20,10};
        int tick = 0;

        public MyTimer(int to,Timeout cb){
            this.timeout = to;
            this.callback = cb;
            timer = new Timer();
            timer.schedule(this,10,interval);// 50ms 调度
        }

        @Override
        public void run() {
            Log.e(TAG,"++");
            if(tick ++ >= timeout){
                tick = 0;
                if(callback!=null){
                    callback.onTimeout();
                }
            }
        }

        @Override
        public boolean cancel() {
            timer.cancel();
            return super.cancel();
        }

        void accelerate(){
            interval = speedUp[acc_level++ % speedUp.length];
            timer.schedule(this,10,interval);
        }
    }

    interface Timeout{
        void onTimeout();
    }
}
